﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Globalization;
using Libs.HR;

public partial class Pages_Hr_Employees_Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AppUtils.CheckLogin();
        Title = Title + " - Thêm mới nhân viên";
        if (!IsPostBack)
        {
            GetDropDownList();
        }
    }

    private void GetDropDownList()
    {
        // Department
        var _Department = new Departments();
        drpDepartment.DataSource = _Department.GetTList();
        drpDepartment.DataBind();
        drpDepartment.Items.Insert(0, new ListItem("Chọn phòng ban:", "0"));

        // Office
        var _Office = new Office();
        drpOffice.DataSource = _Office.GetTList();
        drpOffice.DataBind();
        drpOffice.Items.Insert(0, new ListItem("Chọn văn phòng:", "0"));

        // Loại hợp đồng
        var _ContractType = new ContractType();
        drpContractType.DataSource = _ContractType.GetTList();
        drpContractType.DataBind();
        drpContractType.Items.Insert(0, new ListItem("Chọn loại hợp đồng:", "0"));
    }

    protected void btAdd_Click(object sender, EventArgs e)
    {
        DateTime D;
        var _Employee = new Employees();
        if (!string.IsNullOrEmpty(txtFirstName.ToString()) && !string.IsNullOrEmpty(txtLastName.ToString()))
        {
            _Employee.FirstName = txtFirstName.Text.Trim();
            _Employee.LastName = txtLastName.Text.Trim();
        }
        else return;
        _Employee.ManualID = txtManualID.Text.Trim();
        _Employee.Avatar = Convert.ToInt32(lblFileID.Text);
        if (!string.IsNullOrEmpty(txtYear.ToString()))
        {
            CultureInfo vn = new CultureInfo("vi-VN",true);
            string CombinedDate = String.Format("{0}/{1}/{2}", drpDate.SelectedValue.ToString(), drpMonth.SelectedValue.ToString(), txtYear.Text);
            if (DateTime.TryParseExact(CombinedDate, "dd/MM/yyyy", vn, DateTimeStyles.None, out D))
            {
                _Employee.Birthday = Convert.ToDateTime(D);
            }
            else return;
        }
        else return;

       
        _Employee.Gender = Convert.ToInt32(drpGender.SelectedValue);
        _Employee.Email = txtEmail.Text.Trim();
        _Employee.DepartmentID = Convert.ToInt32(drpDepartment.SelectedValue);
        _Employee.OfficeID = Convert.ToInt32(drpOffice.SelectedValue);
        _Employee.JoinedTime = Convert.ToDateTime(txtJoinedTime.Text, cul);
        _Employee.ContractTypeID = Convert.ToInt32(drpContractType.SelectedValue);
        _Employee.WorkPhone = txtWorkPhone.Text.Trim();
        _Employee.Mobile = txtMobile.Text.Trim();
        _Employee.Add();

        Response.Redirect(Constant.ADMIN_PATH + Resources.Url.EmployeesEdit);
    }

    protected void btCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Constant.ADMIN_PATH + Resources.Url.EmployeesList);
    }

    protected void btImgUpload_Click(object sender, EventArgs e)
    {
        if (fuFileUpload.PostedFile != null && fuFileUpload.PostedFile.ContentLength != 0)
        {
            var _FileData = new FileData();
            _FileData.FileType = fuFileUpload.PostedFile.ContentType;
            _FileData.Size = fuFileUpload.PostedFile.ContentLength;
            _FileData.FileName = Path.GetFileName(fuFileUpload.PostedFile.FileName);

            _FileData.Data = new byte[_FileData.Size];
            fuFileUpload.PostedFile.InputStream.Read(_FileData.Data, 0, _FileData.Size);
            _FileData.Add();
            lblFileID.Text = _FileData.FileID.ToString();
            imgAvatar.ImageUrl = Constant.ADMIN_PATH + Resources.Url.FileDataView + "?id=" + lblFileID.Text;
            imgAvatar.Height = 80;
        }
    }
    protected void drpDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
